package com.dkm.nettysocketdemo.myWebsite.service.impl;


import com.dkm.nettysocketdemo.myWebsite.service.MailService;
import com.dkm.nettysocketdemo.untils.mail.MailBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @ClassName : MailServiceImpl  //类名
 * @Description : 邮箱实现类  //描述
 * @Author : 易嵘  //作者
 * @Date: 2021-01-08 14:08  //时间
 */
@Service
@Slf4j
public class MailServiceImpl implements MailService {

    /**
     * Spring Boot 提供了一个发送邮件的简单抽象，使用的是下面这个接口，这里直接注入即可使用
     */
    @Resource
    private JavaMailSender mailSender;

    /**
     * 设置发件人账号
     */
    @Value("${spring.mail.from}")
    private String from;

    /**
     * 简单文本邮件
     * @param mailBean 邮件对象
     */
    @Override
    public void sendSimpleMail(MailBean mailBean) {
        //创建SimpleMailMessage对象
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件发送人
        message.setFrom(from);
        //邮件接收人
        message.setTo(mailBean.getRecipient());
        //邮件主题
        message.setSubject(mailBean.getSubject());
        //邮件内容
        message.setText(mailBean.getContent());
        //发送邮件
        mailSender.send(message);
    }

    /**
     * html邮件
     * @param mailBean 邮件对象
     */
    @Override
    public void sendHtmlMail(MailBean mailBean) {
        //获取MimeMessage对象
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(message, true);
            //邮件发送人
            messageHelper.setFrom(from);
            //邮件接收人
            messageHelper.setTo(mailBean.getRecipient());
            //邮件主题
            message.setSubject(mailBean.getSubject());
            //邮件内容，html格式
            messageHelper.setText(mailBean.getContent(), true);
            //发送
            mailSender.send(message);
            //日志信息
            log.info("邮件已经发送。");

        } catch (MessagingException e) {
            log.error("发送邮件时发生异常！", e);
        }
    }

    /**
     * 带附件的邮件
     * @param mailBean 邮件对象
     * @param filePath 附件
     */
    @Override
    public void sendAttachmentsMail(MailBean mailBean, String filePath) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(mailBean.getRecipient());
            helper.setSubject(mailBean.getSubject());
            helper.setText(mailBean.getContent(), true);

            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            helper.addAttachment(fileName, file);
            mailSender.send(message);
            //日志信息
            log.info("邮件已经发送。");
        } catch (MessagingException e) {
            log.error("发送邮件时发生异常！", e);
        }
    }
}
